package biblio.view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import biblio.model.Biblio;
import biblio.model.Revue;
import biblio.utils.Search;


/**
 * Classe qui gere l'interface graphique de recherche
 * @author Olivier Dekeyser et Ludovic Fastré
 * @version 29/10/13
 */
public class SearchWindow extends JFrame implements ActionListener {
	
	/**L'outil de recherche utilise dans le GUI*/
	Search search;
	
	Biblio biblio;
	/*
	 * Elements de l'interface graphique
	 */
	private JPanel panel;
	private JPanel searchPanel;
	private JPanel checkBoxPanel;
	private JPanel inventoryPanel;
	private JPanel resultPanel;
	
	private JButton searchButton;
	private JButton triAbc;
	private JButton triRank;
	
	private JTextField searchField;
	
	private JLabel title;
	private JLabel result;
	private JLabel inventory;
	
	private JCheckBox checkTitle;
	private JCheckBox checkRank;
	private JCheckBox checkFor1;
	private JCheckBox checkFor1Name;
	private JCheckBox checkFor2;
	private JCheckBox checkFor2Name;
	private JCheckBox checkFor3;
	private JCheckBox checkFor3Name;
	
	private JTextArea textArea;
	
	/**
	 * Cree un GUI de recherche associe a la bibliotheque passee en 
	 * argument
	 */
	public SearchWindow(Biblio biblio){
		this.biblio = biblio;
		// Initialisation des differents composants du GUI
		search = new Search(biblio);
		panel = new JPanel(new BorderLayout());
		searchPanel = new JPanel(new BorderLayout());
		checkBoxPanel = new JPanel(new GridLayout(2,8));
		inventoryPanel = new JPanel(new BorderLayout());
		resultPanel = new JPanel(new BorderLayout());
		
		searchField = new JTextField();
		searchButton = new JButton("Search");
		
		// Listener
		searchField.getDocument().addDocumentListener(new DocumentListener() {
			  public void changedUpdate(DocumentEvent e) {
				  enableSearchButton();
			  }
			  public void removeUpdate(DocumentEvent e) {
				  enableSearchButton();
			  }
			  public void insertUpdate(DocumentEvent e) {
				  enableSearchButton();
			  }
			  
			  public void enableSearchButton(){
				  if(searchField.getText().equals(""))
						searchButton.setEnabled(false);
				  else
				    	searchButton.setEnabled(true);
			  }
		});
		
		// Si on n'entre rien comme recherche, on desactive le bouton
		if(searchField.getText().equals(""))
			searchButton.setEnabled(false);
		
		//title = new JLabel("Title :");
		result = new JLabel("Result :");
		
		inventory = new JLabel("Inventory :");
		triAbc = new JButton("ABC");
		triRank = new JButton("Rank");
		
		textArea = new JTextArea(5,30);
		textArea.setEditable(false);
		
		checkTitle = new JCheckBox();
		checkTitle.setSelected(true);
		checkTitle.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(checkTitle.isSelected()){
                	checkRank.setSelected(false);
                	checkFor1.setSelected(false);
                	checkFor1Name.setSelected(false);
                	checkFor2.setSelected(false);
                	checkFor2Name.setSelected(false);
                	checkFor3.setSelected(false);
                	checkFor3Name.setSelected(false);
                }
            }
        });
		checkRank = new JCheckBox();
		checkRank.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(checkRank.isSelected()){
                	checkTitle.setSelected(false);
                	checkFor1.setSelected(false);
                	checkFor1Name.setSelected(false);
                	checkFor2.setSelected(false);
                	checkFor2Name.setSelected(false);
                	checkFor3.setSelected(false);
                	checkFor3Name.setSelected(false);
                }
            }
        });
		checkFor1 = new JCheckBox();
		checkFor1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(checkFor1.isSelected()){
                	checkRank.setSelected(false);
                	checkTitle.setSelected(false);
                	checkFor1Name.setSelected(false);
                	checkFor2.setSelected(false);
                	checkFor2Name.setSelected(false);
                	checkFor3.setSelected(false);
                	checkFor3Name.setSelected(false);
                }
            }
        });
		checkFor1Name = new JCheckBox();
		checkFor1Name.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(checkFor1Name.isSelected()){
                	checkRank.setSelected(false);
                	checkFor1.setSelected(false);
                	checkTitle.setSelected(false);
                	checkFor2.setSelected(false);
                	checkFor2Name.setSelected(false);
                	checkFor3.setSelected(false);
                	checkFor3Name.setSelected(false);
                }
            }
        });
		checkFor2 = new JCheckBox();
		checkFor2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(checkFor2.isSelected()){
                	checkRank.setSelected(false);
                	checkFor1.setSelected(false);
                	checkFor1Name.setSelected(false);
                	checkTitle.setSelected(false);
                	checkFor2Name.setSelected(false);
                	checkFor3.setSelected(false);
                	checkFor3Name.setSelected(false);
                }
            }
        });
		checkFor2Name = new JCheckBox();
		checkFor2Name.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(checkFor2Name.isSelected()){
                	checkRank.setSelected(false);
                	checkFor1.setSelected(false);
                	checkFor1Name.setSelected(false);
                	checkFor2.setSelected(false);
                	checkTitle.setSelected(false);
                	checkFor3.setSelected(false);
                	checkFor3Name.setSelected(false);
                }
            }
        });
		checkFor3 = new JCheckBox();
		checkFor3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(checkFor3.isSelected()){
                	checkRank.setSelected(false);
                	checkFor1.setSelected(false);
                	checkFor1Name.setSelected(false);
                	checkFor2.setSelected(false);
                	checkFor2Name.setSelected(false);
                	checkTitle.setSelected(false);
                	checkFor3Name.setSelected(false);
                }
            }
        });
		checkFor3Name = new JCheckBox();
		checkFor3Name.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(checkFor3Name.isSelected()){
                	checkRank.setSelected(false);
                	checkFor1.setSelected(false);
                	checkFor1Name.setSelected(false);
                	checkFor2.setSelected(false);
                	checkFor2Name.setSelected(false);
                	checkFor3.setSelected(false);
                	checkTitle.setSelected(false);
                }
            }
        });
		
		searchButton.addActionListener(this);
		triAbc.addActionListener(this);
		triRank.addActionListener(this);
		
		checkBoxPanel.add(new JLabel("Title:"));
		checkBoxPanel.add(new JLabel("Rank:"));
		checkBoxPanel.add(new JLabel("for1:"));
		checkBoxPanel.add(new JLabel("for1N:"));
		checkBoxPanel.add(new JLabel("for2:"));
		checkBoxPanel.add(new JLabel("for2N:"));
		checkBoxPanel.add(new JLabel("for3:"));
		checkBoxPanel.add(new JLabel("for3N:"));
		checkBoxPanel.add(checkTitle);
		checkBoxPanel.add(checkRank);
		checkBoxPanel.add(checkFor1);
		checkBoxPanel.add(checkFor1Name);
		checkBoxPanel.add(checkFor2);
		checkBoxPanel.add(checkFor2Name);
		checkBoxPanel.add(checkFor3);
		checkBoxPanel.add(checkFor3Name);
		//searchPanel.add(title, BorderLayout.NORTH);
		searchPanel.add(checkBoxPanel, BorderLayout.NORTH);
		searchPanel.add(searchField, BorderLayout.CENTER);
		searchPanel.add(searchButton, BorderLayout.EAST);
		
		inventoryPanel.add(inventory, BorderLayout.NORTH);
		inventoryPanel.add(triAbc, BorderLayout.WEST);
		inventoryPanel.add(triRank, BorderLayout.EAST);
		
		
		JScrollPane scrollPane = new JScrollPane(textArea);
		
		resultPanel.add(result, BorderLayout.NORTH);
		resultPanel.add(scrollPane, BorderLayout.CENTER);
		
		panel.add(searchPanel, BorderLayout.NORTH);
		panel.add(inventoryPanel, BorderLayout.CENTER);
		panel.add(resultPanel, BorderLayout.SOUTH);
		
		this.add(panel);
		
		this.setTitle("Library");
		this.setVisible(true);
		//this.setSize(500, 150);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		pack();
		this.setLocationRelativeTo(null);
	}

	/**
	 * Listener pour le bouton de recherche
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		JButton b = (JButton) e.getSource();
		
		if(b == searchButton){
			if(checkTitle.isSelected()){
				textArea.setText("");
				if(search.searchTitle(searchField.getText()) != null) textArea.setText(search.searchTitle(searchField.getText()).toString());
				else textArea.setText("Title not found or too many occurrences");
			}
			else if(checkRank.isSelected()){
				textArea.setText("");
				if(search.searchRank(searchField.getText()) != null) textArea.setText(search.searchRank(searchField.getText()).toString());
				else textArea.setText("Title not found or too many occurrences");
			}
			else if(checkFor1.isSelected()){
				textArea.setText("");
				if(search.searchFor1(searchField.getText()) != null) textArea.setText(search.searchFor1(searchField.getText()).toString());
				else textArea.setText("Title not found or too many occurrences");
			}
			else if(checkFor1Name.isSelected()){
				textArea.setText("");
				if(search.searchFor1Name(searchField.getText()) != null) textArea.setText(search.searchFor1Name(searchField.getText()).toString());
				else textArea.setText("Title not found or too many occurrences");
			}
			else if(checkFor2.isSelected()){
				textArea.setText("");
				if(search.searchFor2(searchField.getText()) != null) textArea.setText(search.searchFor2(searchField.getText()).toString());
				else textArea.setText("Title not found or too many occurrences");
			}
			else if(checkFor2Name.isSelected()){
				textArea.setText("");
				if(search.searchFor2Name(searchField.getText()) != null) textArea.setText(search.searchFor2Name(searchField.getText()).toString());
				else textArea.setText("Title not found or too many occurrences");
			}
			else if(checkFor3.isSelected()){
				textArea.setText("");
				if(search.searchFor3(searchField.getText()) != null) textArea.setText(search.searchFor3(searchField.getText()).toString());
				else textArea.setText("Title not found or too many occurrences");
			}
			else if(checkFor3Name.isSelected()){
				textArea.setText("");
				if(search.searchFor3Name(searchField.getText()) != null) textArea.setText(search.searchFor3Name(searchField.getText()).toString());
				else textArea.setText("Title not found or too many occurrences");
			}
		}
		
		if(b == triAbc){
			textArea.setText("");
			for(Object currentRevue : biblio.listByTitle()){
				if(currentRevue != null){
					String temporary = textArea.getText();
					textArea.setText(temporary+((Revue)currentRevue).toString()+"\n");
				}
				else textArea.setText("Title not found or too many occurrences");
			}
		}
		
		if(b == triRank){
			textArea.setText("");
			for(Object currentRevue : biblio.listByRank()){
				if(currentRevue != null){
					String temporary = textArea.getText();
					textArea.setText(temporary+((Revue)currentRevue).toString()+"\n");
				}
				else textArea.setText("Title not found or too many occurrences");
			}
		}
		
		/*if(b == searchButton){
			textArea.setText("");
			for(Revue currentRevue : search.searchTitles(searchField.getText()){ // TODO méthode de recherche qui renvoie une liste de Revues
				if(currentRevue != null){
					String temporary = textArea.getText();
					textArea.setText(temporary+currentRevue.toString()+"\n");
				}
				else textArea.setText("Title not found or too many occurrences");
			}
		}*/
	}
	
	/*public static void main(String[] args)
	{
		new SearchWindow();
	}*/
}
